# SPDX-License-Identifier: MIT

# Man pages are only built if they are enabled at configure time.
#
# They should always be built before creating a distribution tarball.

# function which adds the source directory prefix and adds a given suffix
manaddsuffix = $(addsuffix $(1),$(addprefix $(srcdir)/,$(2)))

# List only the names without the .*.txt extension here:
MAN1_NAMES = \
	babeltrace2 \
	babeltrace2-convert \
	babeltrace2-help \
	babeltrace2-list-plugins \
	babeltrace2-query \
	babeltrace2-run
MAN7_NAMES = babeltrace2-filter.utils.muxer \
	babeltrace2-filter.utils.trimmer \
	babeltrace2-intro \
	babeltrace2-plugin-ctf \
	babeltrace2-plugin-text \
	babeltrace2-plugin-utils \
	babeltrace2-sink.ctf.fs \
	babeltrace2-sink.text.pretty \
	babeltrace2-sink.text.details \
	babeltrace2-sink.utils.counter \
	babeltrace2-sink.utils.dummy \
	babeltrace2-source.ctf.fs \
	babeltrace2-source.ctf.lttng-live \
	babeltrace2-source.text.dmesg \
	babeltrace2-query-babeltrace.support-info \
	babeltrace2-query-babeltrace.trace-infos
MAN1_NO_ASCIIDOC_NAMES =
MAN7_NO_ASCIIDOC_NAMES =

if ENABLE_DEBUG_INFO
MAN7_NAMES += \
	babeltrace2-plugin-lttng-utils \
	babeltrace2-filter.lttng-utils.debug-info
endif

# AsciiDoc sources and outputs
MAN1_TXT = $(call manaddsuffix,.1.txt,$(MAN1_NAMES))
MAN7_TXT = $(call manaddsuffix,.7.txt,$(MAN7_NAMES))
MAN_TXT = $(MAN1_TXT) $(MAN7_TXT)
MAN_XML = $(patsubst $(srcdir)/%.txt,%.xml,$(MAN_TXT))

# common AsciiDoc source files
COMMON_TXT = \
	$(srcdir)/common-cli-env.txt \
	$(srcdir)/common-cli-files.txt \
	$(srcdir)/common-cmd-footer.txt \
	$(srcdir)/common-cmd-info-options.txt \
	$(srcdir)/common-cmd-params-format.txt \
	$(srcdir)/common-common-env.txt \
	$(srcdir)/common-footer.txt \
	$(srcdir)/common-gen-options.txt \
	$(srcdir)/common-lib-env.txt \
	$(srcdir)/common-see-babeltrace2-intro.txt \
	$(srcdir)/common-convert-examples.txt \
	$(srcdir)/common-log-levels.txt \
	$(srcdir)/common-trimmer-time-format.txt

# config
ASCIIDOC_CONF = $(srcdir)/bt-asciidoc.conf
ASCIIDOC_ATTRS_CONF = $(builddir)/asciidoc-attrs.conf
XSL_SRC_FILE = $(srcdir)/manpage.xsl

# common dependencies
COMMON_DEPS = $(ASCIIDOC_CONF) $(COMMON_TXT)

# man pages destinations
MAN1 = $(addsuffix .1,$(MAN1_NAMES))
MAN7 = $(addsuffix .7,$(MAN7_NAMES))
MAN1_NO_ASCIIDOC = $(addsuffix .1,$(MAN1_NO_ASCIIDOC_NAMES))
MAN7_NO_ASCIIDOC = $(addsuffix .7,$(MAN7_NO_ASCIIDOC_NAMES))
MAN = $(MAN1) $(MAN7)

if ENABLE_MAN_PAGES
# at this point, we know the user asked to build the man pages
if HAVE_ASCIIDOC_XMLTO
asciidoc_verbose = $(asciidoc_verbose_@AM_V@)
asciidoc_verbose_ = $(asciidoc_verbose_@AM_DEFAULT_V@)
asciidoc_verbose_0 = @echo "  ASCIIDOC  " $@;
xmlto_verbose = $(xmlto_verbose_@AM_V@)
xmlto_verbose_ = $(xmlto_verbose_@AM_DEFAULT_V@)
xmlto_verbose_0 = @echo "  XMLTO     " $@;

# tools
ADOC = $(asciidoc_verbose)$(ASCIIDOC) -f $(ASCIIDOC_CONF) -f $(ASCIIDOC_ATTRS_CONF) -d manpage
ADOC_DOCBOOK = $(ADOC) -b docbook
XTO = $(xmlto_verbose)$(XMLTO) -m $(XSL_SRC_FILE) man

# only add this dependency if we can build the man pages because it's
# a file generated by the configure script, so it's more recent than
# the pregenerated man pages in a tarball
COMMON_DEPS += $(ASCIIDOC_ATTRS_CONF)

# recipes
%.1.xml: $(srcdir)/%.1.txt $(COMMON_DEPS)
	$(ADOC_DOCBOOK) -o $@ $<

%.1: %.1.xml $(XSL_SRC_FILE)
	$(XTO) $< 2>/dev/null

%.7.xml: $(srcdir)/%.7.txt $(COMMON_DEPS)
	$(ADOC_DOCBOOK) -o $@ $<

%.7: %.7.xml $(XSL_SRC_FILE)
	$(XTO) $< 2>/dev/null

# only clean the generated files if we have the tools to generate them again
CLEANFILES = $(MAN_XML) $(MAN)
else # HAVE_ASCIIDOC_XMLTO
# create man page targets used to stop the build if we want to
# build the man pages, but we don't have the necessary tools to do so
ERR_MSG = "Error: Cannot build target because asciidoc or xmlto tool is missing."
ERR_MSG += "Make sure both tools are installed and run the configure script again."

%.1: $(srcdir)/%.1.txt $(COMMON_DEPS)
	@echo $(ERR_MSG)
	@false

%.7: $(srcdir)/%.7.txt $(COMMON_DEPS)
	@echo $(ERR_MSG)
	@false
endif # HAVE_ASCIIDOC_XMLTO
endif # ENABLE_MAN_PAGES

# those are always installed since they are directly written in troff
dist_man1_MANS = $(MAN1_NO_ASCIIDOC)
dist_man7_MANS = $(MAN7_NO_ASCIIDOC)

if ENABLE_MAN_PAGES
# building man pages: we can install and distribute them
dist_man1_MANS += $(MAN1)
dist_man7_MANS += $(MAN7)
endif # ENABLE_MAN_PAGES

if !ENABLE_MAN_PAGES
dist-hook:
	@echo "Error: Please enable the man pages before creating a distribution tarball."
	@false
endif # !ENABLE_MAN_PAGES

# always distribute the source files
EXTRA_DIST = README.adoc $(MAN_TXT) $(COMMON_TXT) $(XSL_SRC_FILE) \
	$(ASCIIDOC_CONF) $(ASCIIDOC_ATTRS_CONF).in
